package com.client.comparator;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	public static long getClickNum(String str1) {
		long value = 0L;
		try {
			value = Long.parseLong(str1);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return value;
	}
	public static int getPersentNum(String str1) {
		int value = 0;
		try {
			value = Integer.parseInt(str1);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return value;
	}
	public static String getDateFromHtml(String html) {
		String dateStr = "";
		Pattern p = Pattern.compile("(\\d{4})-(\\d{1,2})-(\\d{1,2})");
		Matcher m = p.matcher(html);
		if (m.find()) {
			dateStr = m.group();
		} else {
			dateStr = "";
		}
		return dateStr;

	}

	public static double getIndexValue(String str1) {
		double value = 0.0d;
		if (!"".equals(str1) && str1.length() > 0) {
			if (containUnit(str1)) {
				if (contanStr(str1)) {
					str1 = str1.substring(0, getSplitIndex(str1));
					if (containUnit(str1)) {
						str1 = str1.substring(0, getUnitIndex(str1));
					}
					try {
						value = Double.parseDouble(str1);
					} catch (Exception e) {
						System.out.println(value);
					}

				} else {
					str1 = str1.substring(0, getUnitIndex(str1));
					try {
						value = Double.parseDouble(str1);
					} catch (Exception e) {
						System.out.println(str1);
					}

				}

			}
		} else {
			value = 0.0d;
		}
		return value;
	}

	private static int getUnitIndex(String str1) {
		int index = str1.indexOf("万");
		return index;
	}

	private static boolean contanStr(String str1) {
		int index = str1.indexOf("-");
		if (index == -1) {
			return false;
		} else {
			return true;
		}
	}

	private static boolean containUnit(String str1) {
		int index = str1.indexOf("万");
		if (index == -1) {
			return false;
		} else {
			return true;
		}

	}

	private static int getSplitIndex(String string) {
		int index = string.indexOf("-");
		return index;
	}

	public static int getGBCode(char c) {
		byte[] bytes = null;
		try {
			bytes = new StringBuffer().append(c).toString().getBytes("gbk");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if (bytes.length == 1) {
			return bytes[0];
		}
		int a = bytes[0] - 0xA0 + 256;
		int b = bytes[1] - 0xA0 + 256;

		return a * 100 + b;
	}

}
